package com.gatesim;

import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Point;

public class Volt extends Gate{
	
	private static int initResources = 0;
	private static Bitmap onePic;
	private static Bitmap onePicSelected;
	private static Bitmap zeroPic;
	private static Bitmap zeroPicSelected;
	private static int volt_width;
	private static int volt_height;
    private int charge = -1;
	
	public Volt(Point pt, int chrg){
		super(pt);
		center = pt;
		if(initResources == 0){
			initVolt();
			initResources++;
		}
		fntPin = new OutputConnection(
				new Point(center.x + volt_width/2, center.y));
		topPin = null;
		botPin = null;
		setCharge(chrg);
	}

	public static void initVolt(){
	    BitmapFactory.Options options = new BitmapFactory.Options();
	    options.inTempStorage = new byte[16*1024];
	    
	    // TODO: create a using cellphone conditional
	    if ( false ){
	        onePic = BitmapFactory.decodeResource(resources, R.drawable.one);
		    onePicSelected = BitmapFactory.decodeResource(resources, R.drawable.one_selected);
		    zeroPic = BitmapFactory.decodeResource(resources, R.drawable.zero);
		    zeroPicSelected = BitmapFactory.decodeResource(resources, R.drawable.zero_selected);
	    }else{
	        onePic = BitmapFactory.decodeResource(resources, R.drawable.one_cp);
	        onePicSelected = BitmapFactory.decodeResource(resources, R.drawable.one_selected_cp);
	        zeroPic = BitmapFactory.decodeResource(resources, R.drawable.zero_cp);
	        zeroPicSelected = BitmapFactory.decodeResource(resources, R.drawable.zero_selected_cp);	        
	    }
		
		volt_width = onePic.getHeight();
		volt_height = onePic.getWidth();
	}
	

	public void toggleCharge(){
		setCharge((getCharge()+1)%2);
	}
	
	public void draw(Canvas c, Resources r, boolean simulationMode){
		if(getCharge() == 1){//CONN_ONE_CHARGE){
			if(selected){
				c.drawBitmap(onePicSelected, getCenter().x - volt_width/2, getCenter().y - volt_height/2, null);
			} else {
				c.drawBitmap(onePic, getCenter().x - volt_width/2, getCenter().y - volt_height/2, null);
			}
		}else if(getCharge() == 0){//CONN_ZERO_CHARGE){
			if(selected){
				c.drawBitmap(zeroPicSelected, getCenter().x - volt_width/2, getCenter().y - volt_height/2, null);	
			} else {
				c.drawBitmap(zeroPic, getCenter().x - volt_width/2, getCenter().y - volt_height/2, null);	
			}
		}
		fntPin.draw(c, r);
	}
	
	@Override
    public boolean isGate() {return false;}
	public boolean isVolt()	{return true;}
	
	public boolean hasCharge()	{return true;}
	
	public void setCharge(int c) {
		charge = c;
		fntPin.setCharge(c);
		for(Wire w : children){
			w.relayCharge();
		}
	}	    
	
	public int getCharge() {return charge;}
    @Override
    public int produceOut() { return charge; }
    
    public boolean addParent(Gate g) {return false;}
    @Override
    public void recalcPins() {
    	fntPin.setPoint(center.x + volt_width/2, center.y);
    }
    @Override
    public int getWidth() {return volt_width;}
    @Override
    public int getHeight() {return volt_height;}
    
    @Override
    public void select(boolean b, boolean simulationMode){
        selected = b;
    }

	@Override
	public void endSimulationMode() {
		// TODO Auto-generated method stub
		
	}
    
}
